package textfield;

import java.util.Hashtable;
import java.util.Vector;


/**
 * Answers.java
 *
 *
 * Created: Fri Jul 30 10:35:59 2010
 *
 * @author <a href="mailto:fred@theseibels.com">Fred Seibel</a>
 * @version 1.0
 */
public class Answers  implements Operation {
    Hashtable dests = new Hashtable ();
    Hashtable list = new Hashtable ();
    Vector sortedList = new Vector();
    int cnt = 1;
    int choice;
    public Answers() {
	
    } // Answers constructor
    void setChoice(Integer choice) {
	this.choice = choice.intValue() ;
    }
    public Object put(Object answer, Object dest) {
	dests.put(answer, dest);
	sortedList.addElement(answer);
	list.put(new Integer(cnt++), answer);
	return list;
    }
    public final Operation doOperation(final Device device) {
	Answer answer = (Answer) sortedList.elementAt(choice);
	if (answer.x != null) {
	    Proto.recordAnswers.put("x", answer.x.trim());
	    Proto.recordAnswers.put("d", "" + answer.n);
	}
	if (answer == null) {
	    return Proto.nextOp;
	}
	else {
	    return  (Operation) Proto.operations.get(dests.get(answer));
	}
    }
    Vector getAnswers() {
	return sortedList;
    }
     void sortRows () {
	boolean swapped = true;

    	for (int i = sortedList.size() - 1; i > 0; i--) {
	    swapped = false;
    	    for (int j = 0; j < i; j++) {
		Answer answerA = (Answer) sortedList.elementAt(j);
		Answer answerB = (Answer) sortedList.elementAt(j+1);
    		//int rowA = rowID(j);
    		//int rowB = rowID(j + 1);
    		if (answerA.compareTo(answerB) > 0) {
		    swapped = true;
    		    // rowIDs.setElementAt(new Integer(rowB), j);
    		    // rowIDs.setElementAt(new Integer(rowA), j + 1);
		    sortedList.setElementAt(answerB, j);
		    sortedList.setElementAt(answerA, j+1);
    		}
    	    }
    	}
    }
    

} // Answers
